package procilege.impl.eval;

import com.google.inject.Inject;

import procilege.compute.ExprEvaluationTraveller;
import procilege.compute.PlaceType;
import procilege.compute.PlaceTypeMetadata;
import procilege.impl.ComputationalEntityMgr;
import procilege.lang.Name;
import procilege.lang.RelationalExpr;

@PlaceTypeMetadata(type = RelationalExpr.class)
public class RelationalEvalPlace implements PlaceType<ExprEvaluationTraveller> {

	@Inject
	private ComputationalEntityMgr cem;

	public void visit(ExprEvaluationTraveller tr, Object place) {

		RelationalExpr expr = (RelationalExpr) place;

		tr.setout(expr.getLeft());
		Object left = tr.getPreviousVisitResult();
		tr.setout(expr.getRight());
		Object right = tr.getPreviousVisitResult();

		switch (expr.getRelation()) {

		case RelationalExpr.RELATION_EQ:
			tr.result(left.equals(right));
			return;

		case RelationalExpr.RELATION_NEQ:
			tr.result(!left.equals(right));
			return;

		case RelationalExpr.RELATION_CATEGORY:
			Name ln = (Name) left;
			Name rn = (Name) right;

			ln = (Name) left;
			rn = (Name) right;
			
			tr.result(cem.isa(ln, rn));
			return;

		default:
			throw new UnsupportedOperationException();
		}
	}
}
